package com.uphone.kingmall.activity;

import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.PopupWindow;

import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.china.zhuoyue.R;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.uphone.kingmall.adapter.HaggleGoodsAdapter;
import com.uphone.kingmall.base.BaseListActivity;
import com.uphone.kingmall.bean.GoodsDetailBean;
import com.uphone.kingmall.bean.GoodsListBean;
import com.uphone.kingmall.dialog.ShopCartPw;
import com.uphone.kingmall.listener.onNormalRequestListener;
import com.uphone.kingmall.utils.CommonUtil;
import com.uphone.kingmall.utils.ConstansUtils;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.OkGoUtils;
import com.uphone.kingmall.utils.ToastUtils;
import com.uphone.kingmall.utils.intent.IntentUtils;
import com.uphone.kingmall.utils.navigationbar.DefaultNavigationBar;

import org.json.JSONObject;

import java.util.List;

public class HaggleGoodsListActivity extends BaseListActivity<GoodsListBean, GoodsListBean.DataBean> {

    private ShopCartPw shopCartPw;
    private View ivBack;


    @Override
    protected void setTitle(DefaultNavigationBar navigationBar) {
        ivBack = navigationBar.getLeftIcon();
    }
    @Override
    protected String initTitle() {
        return "砍价购";
    }

    @Override
    protected void itemClick(BaseQuickAdapter adapter, View view, int position) {
        IntentUtils.getInstance().with(this, GoodsDetailActivity.class)
                .putInt(ConstansUtils.ID, dataList.get(position).getGoodsId())
                .putInt(ConstansUtils.TYPE,GoodsDetailActivity.TYPE_HAGGLE).start();
    }

    @Override
    protected BaseQuickAdapter initAdapter() {
        HaggleGoodsAdapter adapter = new HaggleGoodsAdapter();
        adapter.setOnItemHaggleClickListener(new HaggleGoodsAdapter.OnItemHaggleClickListener() {
            @Override
            public void haggle(View view, int position, GoodsListBean.DataBean item) {
                openCartList(item);
            }
        });
        return adapter;
    }

    @Override
    protected GoodsListBean getBeans() {
        return new GoodsListBean();
    }

    @Override
    protected HttpParams getParams() {
        HttpParams params = new HttpParams();
        params.put("page", page);
        params.put("isIndex", 0);
        params.put("limit", 10);
        return params;
    }

    @Override
    protected String getUrl() {
        return MyUrl.getHaggleGoods;
    }
    private void openCartList(final GoodsListBean.DataBean bean) {//1 加入购物车 2规格
        LogUtils.e("bean:"+bean);
        if (bean == null ) {
            ToastUtils.showShortToast(this, "未找到商品");
            return;
        }
        shopCartPw = new ShopCartPw(HaggleGoodsListActivity.this, new GoodsDetailBean.GoodsBean(bean), null,new ShopCartPw.setOnDialogClickListener() {
            @Override
            public void onClick(View v, int type, String pos, String num, String price) {
                switch (v.getId()) {
                    case R.id.pw_cart_btn://确定
                        break;
                }
            }
            @Override
            public void onClick(View v, int type, List<GoodsDetailBean.GoodsBean.PropsBean> propsList, List<GoodsDetailBean.GoodsBean.PropsBean.PropBean> propList, String num, String price) {
                switch (v.getId()) {
                    case R.id.pw_cart_btn://确定
                        if (type == 666) {
                            //发起砍价
                            launchHaggle(bean,propList, price, num);
                        }
                        break;
                }
            }
        }, new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setStatusBar();
            }

        });
        shopCartPw.showAtLocation(shopCartPw.popupView.getRootView(), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    private void launchHaggle(GoodsListBean.DataBean bean,final List<GoodsDetailBean.GoodsBean.PropsBean.PropBean> propList, final String price, final String num) {
        HttpParams params = new HttpParams();
        params.put("goodsId", bean.getGoodsId());

        if (propList != null) {
            String goodsPropIds = "";
            for (int i = 0; i < propList.size(); i++) {
                goodsPropIds += propList.get(i).getGoodsPropId() + ",";
            }
            if (!TextUtils.isEmpty(goodsPropIds)) {
                String str = goodsPropIds.substring(0, goodsPropIds.length() - 1);
                params.put("goodsPropId", str);
            }
        }
        OkGoUtils.progressRequest(MyUrl.launchHaggle, this, params, new onNormalRequestListener() {
            @Override
            public void onSuccess(String response, int code) {
                if (code == 0 || code == 1) {
                    if (code == 0) {
                        ToastUtils.showShortToast(HaggleGoodsListActivity.this, "成功发起砍价");
                    }
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        int haggleId = jsonObject.getInt("haggleId");
                        LogUtils.e("haggleId:"+haggleId);
                        CommonUtil.startIntent(HaggleGoodsListActivity.this, HaggleDetailActivity.class, haggleId);
                    } catch (Exception e) {
                        LogUtils.e("异常:" + e.toString());
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }

}
